package cn.fczds.camhelper.user.service.impl;

import cn.fczds.camhelper.base.exception.ServiceException;
import cn.fczds.camhelper.base.response.StatusCode;
import cn.fczds.camhelper.user.mapper.UserMapper;
import cn.fczds.camhelper.user.pojo.dto.*;
import cn.fczds.camhelper.user.pojo.entity.User;
import cn.fczds.camhelper.user.pojo.vo.UserVO;
import cn.fczds.camhelper.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录，参数：{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void reg(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);
        user.setNickname("user");
        user.setAdminId(1L);
        user.setGender("未知");
        user.setPhone("绑定手机号吧~");
        user.setPosition("设置常用地址吧~");
        user.setBirth(new Date());
        user.setEmail("设置邮箱吧~");
        user.setLevel(10L);
        user.setRiderLevel(0L);
        user.setDescription("记住要仰望星空，不要低头看脚下。无论生活如何艰难，请保持一颗好奇心！");
        user.setMessage("这个真的很方便！");
        user.setAvatarImgUrl("messageAvatar/默认头像.jpg");
        log.debug("注册user:{}",user);
        userMapper.reg(user);
    }

    @Override
    public List<UserVO> select(UserInfoQuery userInfoQuery) {
        log.debug("userInfoQuery={}",userInfoQuery);
        List<UserVO> list = userMapper.selectUserInfo(userInfoQuery);
        return list;
    }

    @Override
    public List<UserVO> update(UserUpdateParam userUpdateParam) {
        User user = new User();
        UserInfoQuery userInfoQuery = new UserInfoQuery();
        userInfoQuery.setUsername(userUpdateParam.getUsername());
        BeanUtils.copyProperties(userUpdateParam, user);
        log.debug("userUpdate:{}",user);
        userMapper.update(user);
        List<UserVO> list = userMapper.selectUserInfo(userInfoQuery);
        log.debug("list:{}",list);
        return list;
    }

    @Override
    public void insertMessage(UserMessageParam userMessageParam) {
        User user = new User();
        BeanUtils.copyProperties(userMessageParam, user);
        user.setUsername("user");
        user.setPassword("123456");
        user.setAvatarImgUrl("头像1.jpg.gif");
        userMapper.insertMessage(user);
    }


}
